﻿#include "kdpch.h"
#include "SubTexture2d.h"

namespace KD {

	SubTexture2D::SubTexture2D(const Ref<Texture2D>& texture, const glm::vec2& min, const glm::vec2& max)
		: m_Texture(texture)
	{
		// 精灵的位置
		m_TexCoords[0] = { min.x, min.y }; //精灵的左下角x,y坐标
		m_TexCoords[1] = { max.x, min.y }; //精灵的右下角x,y坐标
		m_TexCoords[2] = { max.x, max.y }; //精灵的右上角x,y坐标
		m_TexCoords[3] = { min.x, max.y }; //精灵的左上角x,y坐标	
	}

	Ref<SubTexture2D> SubTexture2D::CreateFormCoords(const Ref<Texture2D>& texture, const glm::vec2& coords, const glm::vec2& cellSize, const glm::vec2& spriteSize)
	{
		// cellSize.x精灵的宽， cellSize.y精灵的高, coords.x行,coords.y列, spriteSize下标:每+1翻1倍
		glm::vec2 min = { coords.x * cellSize.x / texture->GetWidth(), coords.y * cellSize.y / texture->GetHeight() };
		glm::vec2 max = { (coords.x + spriteSize.x) * cellSize.x / texture->GetWidth(), (coords.y + spriteSize.y) * cellSize.y / texture->GetHeight() };

		return CreateRef<SubTexture2D>(texture, min, max);
	}

}